package com.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("stu")   // 给 api 加前缀,  stu/get, stu/create ...
@Slf4j
public class StuController {
    @GetMapping("{stuId}/get")  //匹配 http://localhost:8081/stu/test/get?id=1&name=b
    public String getStu(
            @PathVariable("stuId") String stuId,  // 路径参数匹配, 上述 url 中的 test
            @RequestParam Integer id, // 接收 param -> id
            @RequestParam String name // 接收 param -> name
    ) {
        return stuId + "," + id + "," + name;
    }

    @PostMapping("create")
    public String createStu(
            @RequestBody Map<String, Object> map, // 前台传过来的 post 参数
            @RequestHeader("token") String token, // 请求头
            @CookieValue("clientId") String clientId, // 请求时的 cookie
            HttpServletRequest request
    ) {
        log.info("map:" + map.toString());
        log.info("token:" + token);
        log.info("clientId:", clientId);

        // 另一种获取 RequestHeader 的方法
        String headerToken = request.getHeader("token");

        log.info("headerToken:", headerToken);
        return "create Stu";
    }

    @PutMapping("update")
    public String updateStu() {
        return "update Stu";
    }

    @DeleteMapping("delete")
    public String deleteStu() {
        return "delete Stu";
    }
}
